package Program.UserUI;

import Program.Information.Fee;
import Program.Information.User;
import Program.SqlTool.UserDAO;
import Program.Tool.Popup;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;


public class FeePaneSon1 extends JPanel {
    private User user;
    private JScrollPane jScrollPane;
    private JTable table;
    private DefaultTableModel dtm;
    private String[] titles = {"类型", "信息"};
    private Object[][] data;
    //日期
    private String date;
    //年
    private int year;
    //月
    private int month;
    //支付按钮
    private JButton payJButton;
    //刷新按钮
    private JButton refreshJButton;
    //设置字体
    private Font font = new Font("微软雅黑", Font.BOLD, 20);
    //上一个月按钮
    private JButton lastMouthJButton;
    //下一个月按钮
    private JButton nextMouthJButton;
    //日期判断标志
    private int flag;
    //费用对象
    private Fee fee;


    public FeePaneSon1() {
        super(null);
        setBounds(0, 0, 900, 600);
        setBackground(Color.WHITE);
        add(initScrollPane());
        initJButton();
    }

    public FeePaneSon1(User user,int year,int month) {
        super(null);
        this.user = user;
        this.year = year;
        this.month = month;
        this.date = year+"/"+month;
        setBounds(0, 0, 900, 600);
        setBackground(Color.WHITE);
        add(initScrollPane());
        initJButton();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(new ImageIcon("Application_images\\userBG2.png").getImage(), 0, 0, this.getWidth(), this.getHeight(), this);
    }

    //初始化按钮
    private void initJButton() {
        //按钮赋予对象
        payJButton = new JButton("支 付");
        refreshJButton = new JButton("刷 新");
        lastMouthJButton = new JButton("上个月");
        nextMouthJButton = new JButton("下个月");
        //设置字体
        payJButton.setFont(font);
        refreshJButton.setFont(font);
        lastMouthJButton.setFont(font);
        nextMouthJButton.setFont(font);

        //支付按钮判断
        if (user.getState().equals("0")) {
            // 账号不可用的情况下，无法正常使用缴费功能
            payJButton.setBackground(Color.DARK_GRAY);
            payJButton.setEnabled(false);
            payJButton.setToolTipText("您的账户不可用,请联系管理员");
        } else if (fee.getState().equals("1")) {
            // 没有欠费单的话 按钮无法使用
            payJButton.setBackground(Color.DARK_GRAY);
            payJButton.setEnabled(false);
            payJButton.setToolTipText("本月账单已支付，按钮无法使用");
        }else if (fee.getState().equals("-1")){
            payJButton.setBackground(Color.DARK_GRAY);
            payJButton.setEnabled(false);
            payJButton.setToolTipText("您的账户无账单");
        } else {
            payJButton.setBackground(new Color(103, 172, 80));
            payJButton.setEnabled(true);
        }
        refreshJButton.setBackground(new Color(191, 230, 240));
        if (fee.getState().equals("-1")){
            lastMouthJButton.setBackground(Color.DARK_GRAY);
            lastMouthJButton.setEnabled(false);
            lastMouthJButton.setToolTipText("不可用");
        }else {
            lastMouthJButton.setBackground(new Color(191, 230, 240));
        }
        if (fee.getState().equals("-1")){
            nextMouthJButton.setBackground(Color.DARK_GRAY);
            nextMouthJButton.setEnabled(false);
            nextMouthJButton.setToolTipText("不可用");
        }else {
            nextMouthJButton.setBackground(new Color(191, 230, 240));
        }

        //设置字体颜色
        payJButton.setForeground(Color.white);
        refreshJButton.setForeground(Color.white);
        lastMouthJButton.setForeground(Color.white);
        nextMouthJButton.setForeground(Color.white);
        //设置按钮的位置
        payJButton.setBounds(285, 480, 80, 65);
        refreshJButton.setBounds(535, 480, 80, 65);
        lastMouthJButton.setBounds(750, 200, 80, 65);
        nextMouthJButton.setBounds(750, 300, 80, 65);
        //取消按钮的焦点框
        payJButton.setFocusPainted(false);
        refreshJButton.setFocusPainted(false);
        lastMouthJButton.setFocusPainted(false);
        nextMouthJButton.setFocusPainted(false);
        //取消按钮的边框
        payJButton.setBorder(null);
        refreshJButton.setBorder(null);
        lastMouthJButton.setBorder(null);
        nextMouthJButton.setBorder(null);
        //设置鼠标样式
        payJButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        refreshJButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        lastMouthJButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        nextMouthJButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

        ActionListener listener = getListener();
        payJButton.addActionListener(listener);
        refreshJButton.addActionListener(listener);
        lastMouthJButton.addActionListener(listener);
        nextMouthJButton.addActionListener(listener);
        this.add(payJButton);
        this.add(refreshJButton);
        this.add(lastMouthJButton);
        this.add(nextMouthJButton);
    }

    public ActionListener getListener() {
        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String info = "";
                if (e.getSource() == payJButton) {
                    if (user.getAdDeposit() >= fee.getSumFee()) {
                        double sum = user.getAdDeposit() - fee.getSumFee();
                        user.setAdDeposit(sum);
                        info = "支付成功";
                        Popup.showJDialog(info);
                        UserDAO.updateAdDeposit(user.getUserName(),sum);
                        UserDAO.updatePayState(fee.getId());
                        refreshJPanel();
                    } else {
                        info = "支付失败，余额不足";
                        Popup.showJDialog(info);
                    }
                } else if (e.getSource() == refreshJButton) {
                    refreshJPanel();
                }else if (e.getSource() == lastMouthJButton){
                    flag =-1;
                    date = getDate(flag,year,month);
                    if (date.equals("最小日期")){
                        info = "上个月无账单";
                        Popup.showJDialog(info);
                    }else{
                        refreshJPanel();
                    }
                }else if (e.getSource() == nextMouthJButton){
                    flag = 1;
                    date = getDate(flag,year,month);
                    if (date.equals("最大日期")){
                        info = "下个月无账单";
                        Popup.showJDialog(info);
                    }else {
                        refreshJPanel();
                    }
                }
            }
        };
        return listener;
    }

    //刷新界面
    private void refreshJPanel(){
        this.removeAll();
        this.repaint();
        this.updateUI();
        fee = UserDAO.lookFee(user.getUserID(), date);
        user = UserDAO.selectUser(user.getUserName());
        this.add(initScrollPane());
        initJButton();
    }

    //获取日期函数
    private String getDate(int flag,int tempYear,int tempMonth){
        ArrayList<String> list = UserDAO.getUsageDay(user.getUserID());
        String minDayStr = list.get(0);
        String maxDayStr = list.get(list.size()-1);
        String[] minArr = minDayStr.split("/");
        String[] maxArr = maxDayStr.split("/");
        int minYear = Integer.parseInt(minArr[0]);
        int minMonth = Integer.parseInt(minArr[1]);
        int maxYear = Integer.parseInt(maxArr[0]);
        int maxMonth = Integer.parseInt(maxArr[1]);
        if (tempYear==maxYear && tempMonth == maxMonth && flag == 1){
            return "最大日期";
        }else if (tempYear == minYear && tempMonth == minMonth && flag == -1){
            return "最小日期";
        }else {
            this.month = month + flag;
            if (month<0){
                this.month = 12;
                this.year = year-1;
            }else if (month>12){
                this.month = 0;
                this.year = year+1;
            }
            return year+"/"+month;
        }
    }

    private JScrollPane initScrollPane() {
        jScrollPane = new JScrollPane();
        jScrollPane.getViewport().setOpaque(false);
        jScrollPane.setOpaque(false);
        jScrollPane.setBounds(200, 50, 500, 390);
        JTable viewTable = getViewTable();
        jScrollPane.setViewportView(viewTable);
        return jScrollPane;
    }

    private JTable getViewTable() {
        data = createArr();
        dtm = new DefaultTableModel(data, titles);
        table = new JTable(dtm) {
            // 重写这个表格的方法：设置不可编辑，但可以选中
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 16));
        table.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        table.setRowHeight(40);
        table.getTableHeader().setReorderingAllowed(false);// 设置列不可拖动
        // 设置居中
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(JLabel.CENTER);
        renderer.setOpaque(false);
        table.setDefaultRenderer(Object.class, renderer);
        table.setOpaque(false);    // 设置表格本身透明
        table.getTableHeader().setOpaque(false);    //设置透明
        return table;
    }

    private Object[][] createArr() {
        fee = UserDAO.lookFee(user.getUserID(), date);
        Object[][] arr = {
                {"用户名", this.user.getUserName()},
                {"业主姓名", this.user.getName()},
                {"水费", fee.getWaterFee() + " 元"},
                {"电费", fee.getElectricFee() + " 元"},
                {"天然气费", fee.getGasFee() + " 元"},
                {"停车费", fee.getParkFee() + " 元"},
                {"日期", fee.getDay()},
                {"总费用", fee.getSumFee() + " 元"},
                {"支付状态", checkPay(fee)}
        };
        return arr;
    }

    //判断是否已经支付
    private String checkPay(Fee fee) {
        if (fee.getState().equals("1")) {
            return "已支付";
        }else if (fee.getState().equals("-1")){
            return "无账单";
        }
        return "未支付";
    }

    public String getDay() {
        return date;
    }

    public void setDay(String day) {
        this.date = day;
    }
}
